package com.example.mapdata.controller;


import com.example.mapdata.entity.Hotcity;
import com.example.mapdata.service.HotcityService;
import com.example.mapdata.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Hotcity)表控制层
 *
 * @author makejava
 * @since 2024-09-20 08:49:25
 */
@Slf4j
@RestController
@RequestMapping("/api/hotcity")
@CrossOrigin
public class HotcityController {
    @Autowired
    private HotcityService hotcityService;

    /**
     * 获取所有城市信息
     *
     * @return
     */
    @GetMapping("get_all_city")
    public ResultVo getAllCity() {
        List<Hotcity> allCity = hotcityService.getAllCity();
        if (allCity != null) {
            return new ResultVo(allCity);
        } else {
            return new ResultVo("获取失败", false);
        }
    }

    /**
     * 根据别名获取指定城市信息
     *
     * @param code
     * @return
     */
    @GetMapping("get_city_by_code")
    public ResultVo getCityByAlias(@RequestParam String code) {
        Hotcity city = hotcityService.getCityByCode(code);
        if (city != null) {
            return new ResultVo(city);
        } else {
            return new ResultVo("获取失败", false);
        }
    }

    /**
     * 添加城市信息
     *
     * @param hotcity
     * @return
     */
    @PostMapping("add_city")
    public ResultVo addCityInfo(@RequestBody Hotcity hotcity) {
        boolean result = hotcityService.addCityInfo(hotcity);
        if (result) {
            return new ResultVo(200, "查询成功", true, hotcity);
        } else {
            return new ResultVo("添加失败", false);
        }
    }

    /**
     * 根据城市名称更新对应城市信息
     *
     * @param name
     * @param hotcity
     * @return
     */
    @PutMapping("update_city")
    public ResultVo updateCityInfo(@RequestParam String name, @RequestBody Hotcity hotcity) {
        boolean b = hotcityService.updateCityInfo(name, hotcity);
        if (b) {
            return new ResultVo(200, "更新成功", true, hotcity);
        } else {
            return new ResultVo("更新失败", false);
        }
    }

    /**
     * 根据城市编号删除对应的城市信息以及区县信息
     *
     * @param cityId
     * @return
     */
    @DeleteMapping("delete_city_by_id")
    public ResultVo deleteCityById(@RequestParam String cityId) {
        boolean result = hotcityService.deleteCityId(cityId);
        log.info("删除结果：{}", result);
        if (result) {
            return new ResultVo("删除成功", true);
        } else {
            return new ResultVo("删除失败", false);
        }
    }
}

